package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.CreatePermissionCommand;
import com.wiscamp.ninechapters.panels.application.command.UpdatePermissionCommand;
import com.wiscamp.ninechapters.panels.application.services.PermissionAppService;
import com.wiscamp.ninechapters.panels.application.vo.PermissionVO;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 权限服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-权限信息")
public class PermissionController extends BaseController {

    private final PermissionAppService permissionAppService;

    public PermissionController(PermissionAppService permissionAppService) {
        this.permissionAppService = permissionAppService;
    }

    @GetMapping("Permissions/{permissionId}")
    @Operation(summary = "通过编号获取权限")
    @HandleResourceException
    public RestResponse<PermissionVO> getPermission(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @PathVariable("permissionId") long permissionId) {
        return ResourceUtils.getById(request, response, permissionId, permissionAppService::getPermission);
    }


    @GetMapping("Applications/{appId}/Permissions/Code/{permissionCode}")
    @Operation(summary = "通过代码获取权限")
    @HandleResourceException
    public RestResponse<PermissionVO> getPermissionByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                          @PathVariable("appId") long appId, @PathVariable("permissionCode") String permissionCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, permissionCode, permissionAppService::getPermissionByCode);
    }

    @GetMapping("Applications/{appId}/Permissions/Name/{permissionName}")
    @Operation(summary = "通过名称获取权限列表")
    @HandleResourceException
    public RestResponse<Collection<PermissionVO>> getPermissionsByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                       @PathVariable("appId") long appId, @PathVariable("permissionName") String permissionName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, permissionName, permissionAppService::getPermissionsByName);
    }

    @GetMapping("Applications/{appId}/Permissions/Group/{permissionGroup}")
    @Operation(summary = "通过分组获取权限列表")
    @HandleResourceException
    public RestResponse<Collection<PermissionVO>> getPermissionsByGroup(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                        @PathVariable("appId") long appId, @PathVariable("permissionGroup") String dictionarGroup) {
        return ResourceUtils.getListByIdAndText(request, response, appId, dictionarGroup, permissionAppService::getPermissionsByGroup);
    }

    @PostMapping("Permissions/Query")
    @Operation(summary = "分页获取权限列表")
    @HandleResourceException
    public RestResponse<Collection<PermissionVO>> getPermissionsByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                        @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, permissionAppService::getPermissionsByQuery);
    }

    @PostMapping("Permissions")
    @Operation(summary = "创建权限")
    @HandleResourceException
    public RestResponse<PermissionVO> createPermission(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreatePermissionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, permissionAppService::createPermission);
    }


    @PutMapping("Permissions")
    @Operation(summary = "更新权限")
    @HandleResourceException
    public RestResponse<PermissionVO> updatePermission(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdatePermissionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, permissionAppService::updatePermission);
    }

    @PutMapping("Permissions/Actions/Delete")
    @Operation(summary = "通过编号删除权限")
    @HandleResourceException
    public RestResponse<Boolean> deletePermission(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, permissionAppService::deletePermission);
    }

    @PutMapping("Permissions/Actions/Enable")
    @Operation(summary = "通过编号启用权限")
    @HandleResourceException
    public RestResponse<Boolean> enablePermission(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, permissionAppService::enablePermission);
    }

    @PutMapping("Permissions/Actions/Disable")
    @Operation(summary = "通过编号禁用权限")
    @HandleResourceException
    public RestResponse<Boolean> disablePermission(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, permissionAppService::disablePermission);
    }
}
